#!/bin/sh
# called from shutdown menu
# config files: ~/.jwmrc, etc
# script: wmexit -- symlinks: wmreboot wmpoweroff restartwm

export TEXTDOMAIN=wmexit
export OUTPUT_CHARSET=UTF-8

script=${0##*/}
script2=$1

case $script in wmexit|wmreboot|wmpoweroff|restartwm) OK=1 ;; esac
case $script2 in wmexit|wmreboot|wmpoweroff|restartwm) script=$script2 ; shift ; OK=1 ;; esac
[ ! "$OK" ] && echo "ERROR: $script is not valid" && exit 1
echo $script

[ -f /etc/wmexit.conf ] && . /etc/wmexit.conf
if [ "$WMEXIT_SPLASH" = "yes" ] ; then
	[ $WMEXIT_SPLASH_BG ] && BG="-bg $WMEXIT_SPLASH_BG"
	[ $WMEXIT_SPLASH_FG ] && FG="-fg $WMEXIT_SPLASH_FG"
	case $script in
		restartwm)  SMSG="$(gettext 'Restarting window manager....')" ;;
		wmexit)     SMSG="$(gettext 'Exiting to console....')" ;;
		wmreboot)   SMSG="$(gettext 'Rebooting....')" ;;
		wmpoweroff) SMSG="$(gettext 'Powering down....')" ;;
	esac
	yaf-splash -placement top $BG $FG -timeout 10 -text "$SMSG" &
fi

if pidof lxpanel &>/dev/null ; then #probably running LXDE
	#http://wiki.lxde.org/en/LXPanel#Fix_empty_menu_in_LXPanel
	find ~/.cache/menus -name '*' -type f -delete
fi

#--------------
case $script in
   restartwm)
      ## Exit from window manager ##
      # will cause return to xwin, which reads
      # /tmp/wmexitmode.txt and will relaunch w.m.
      NEXTWM="$1"
      [ $NEXTWM ] || NEXTWM="`cat /etc/windowmanager`" #precaution.
      echo -n "$NEXTWM" > /etc/windowmanager #this makes change permanent.
      EXITMODE="$NEXTWM"
      ;;
   wmexit)
      ## Exit from X, will cause return to xwin ##
      EXITMODE="exit"
      ;;
   wmreboot|wmpoweroff)
      ## reboot/shutdown ##
      . /etc/rc.d/PUPSTATE
      if [ $PUPMODE -eq 5 ] && [ $DISPLAY ] ; then #first shutdown.
         shutdownconfig #dlgs for creating save-file.
         #...writes results to /tmp/shutdownconfig_results, which /etc/rc.d/rc.shutdown reads.
         [ $? -eq 255 ] && exit #abort shutdown.
      fi
      [ "$script" = 'wmreboot' ] && EXITMODE="reboot"
      [ "$script" = 'wmpoweroff' ] && EXITMODE="poweroff"
      ;;
esac

#/sbin/pup_event_frontend_d will quit if this file exists...
echo -n "$EXITMODE" > /tmp/wmexitmode.txt

###########################################################

while [ "`pidof snapmergepuppy`" ] ; do sleep 1 ; done
sleep 0.2

sync

         #xfce       #lxde     #window managers
for i in xfce4-panel lxsession `cat /etc/windowmanager` jwm icewm openbox
do
	for pid in `pidof $i` ; do
		kill $pid || kill -9 $pid
	done
done

### END ###